package com.ruoyi.gczl.sybg.dq.spyj.service.impl;

import com.ruoyi.common.annotation.DataScope;
import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.gczl.sybg.dq.spyj.domain.SybgSpyjdq;
import com.ruoyi.gczl.sybg.dq.spyj.mapper.SybgSpyjdqMapper;
import com.ruoyi.gczl.sybg.dq.spyj.service.ISybgSpyjdqService;
import org.apache.ibatis.session.ExecutorType;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import javax.annotation.Resource;
import java.util.List;

/**
 * 试验报告沙坪一级电气Service业务层处理
 *
 * @author huacai
 * @date 2024-08-22
 */
@Service
public class SybgSpyjdqServiceImpl implements ISybgSpyjdqService {
    @Resource
    private SybgSpyjdqMapper sybgSpyjdqMapper;

    @Resource
    private SqlSessionFactory sqlSessionFactory;

    /**
     * 查询试验报告沙坪一级电气
     *
     * @param sybgSpyjdqId 试验报告沙坪一级电气主键
     * @return 试验报告沙坪一级电气
     */
    @Override
    public SybgSpyjdq selectSybgSpyjdqBySybgSpyjdqId(Integer sybgSpyjdqId) {
        return sybgSpyjdqMapper.selectSybgSpyjdqBySybgSpyjdqId(sybgSpyjdqId);
    }

    /**
     * 查询试验报告沙坪一级电气列表
     *
     * @param sybgSpyjdq 试验报告沙坪一级电气
     * @return 试验报告沙坪一级电气
     */
    @Override
    @DataScope(deptAlias = "t1")
    public List<SybgSpyjdq> selectSybgSpyjdqList(SybgSpyjdq sybgSpyjdq) {
        return sybgSpyjdqMapper.selectSybgSpyjdqList(sybgSpyjdq);
    }

    /**
     * 新增试验报告沙坪一级电气
     *
     * @param sybgSpyjdq 试验报告沙坪一级电气
     * @return 结果
     */
    @Override
    public int insertSybgSpyjdq(SybgSpyjdq sybgSpyjdq) {
        sybgSpyjdq.setCreateTime(DateUtils.getNowDate());
        return sybgSpyjdqMapper.insertSybgSpyjdq(sybgSpyjdq);
    }

    /**
     * 批量新增试验报告沙坪一级电气
     *
     * @param sybgSpyjdqs 试验报告沙坪一级电气List
     * @return 结果
     */
    @Override
    public int batchInsertSybgSpyjdq(List<SybgSpyjdq> sybgSpyjdqs) {
        SqlSession sqlSession = sqlSessionFactory.openSession(ExecutorType.BATCH, false);
        int count = 0;
        if (!CollectionUtils.isEmpty(sybgSpyjdqs)) {
            try {
                for (int i = 0; i < sybgSpyjdqs.size(); i++) {
                    int row = sybgSpyjdqMapper.insertSybgSpyjdq(sybgSpyjdqs.get(i));
                    // 防止内存溢出，每100次提交一次,并清除缓存
                    boolean bool = (i > 0 && i % 100 == 0) || i == sybgSpyjdqs.size() - 1;
                    if (bool) {
                        sqlSession.commit();
                        sqlSession.clearCache();
                    }
                    count = i + 1;
                }
            } catch (Exception e) {
                e.printStackTrace();
                // 没有提交的数据可以回滚
                sqlSession.rollback();
            } finally {
                sqlSession.close();
                return count;
            }
        }
        return count;
    }

    /**
     * 修改试验报告沙坪一级电气
     *
     * @param sybgSpyjdq 试验报告沙坪一级电气
     * @return 结果
     */
    @Override
    public int updateSybgSpyjdq(SybgSpyjdq sybgSpyjdq) {
        sybgSpyjdq.setUpdateTime(DateUtils.getNowDate());
        return sybgSpyjdqMapper.updateSybgSpyjdq(sybgSpyjdq);
    }

    /**
     * 批量删除试验报告沙坪一级电气
     *
     * @param sybgSpyjdqIds 需要删除的试验报告沙坪一级电气主键
     * @return 结果
     */
    @Override
    public int deleteSybgSpyjdqBySybgSpyjdqIds(Integer[] sybgSpyjdqIds) {
        return sybgSpyjdqMapper.deleteSybgSpyjdqBySybgSpyjdqIds(sybgSpyjdqIds);
    }

    /**
     * 删除试验报告沙坪一级电气信息
     *
     * @param sybgSpyjdqId 试验报告沙坪一级电气主键
     * @return 结果
     */
    @Override
    public int deleteSybgSpyjdqBySybgSpyjdqId(Integer sybgSpyjdqId) {
        return sybgSpyjdqMapper.deleteSybgSpyjdqBySybgSpyjdqId(sybgSpyjdqId);
    }
}
